// pages/fetchMoney/fetchMoney.js
const httpUtils = require('../../utils/httpUtils.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: 'https://yxpmaterial.sse100.com/mall/',
    name: '',
    bankCode: '',
    bankName: '',
    totleMoney:'',
    money: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.getbankinfo({
      access_token: app.globalData.$TOKEN,
    }).then((res) => {
      wx.hideLoading()
      that.setData({
        name: res.data.invoice_name,
        bankCode: res.data.invoice_bank_account,
        bankName: res.data.invoice_bank,
        money: res.data.amount,
        totleMoney:res.data.amount
      })

    });


  },

  bindMoneyChange: function (e) {
    let that = this;
    that.setData({
      money: e.detail.value
    })

  },
  goSave: function () {
    console.log("money:" + this.data.money+"totleMoney:"+this.data.totleMoney)
    var num1 = Number(this.data.money )
var num2 = Number(this.data.totleMoney)
    if ( num1 == 0) {
      wx.showToast({
        title: "提现金额不能小于等于0",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(num1>num2){
      wx.showToast({
        title: "提现金额超限",
        icon: 'none',
        duration: 2000
      })
      return
    }
    this.withdraw()
  },
  withdraw: function () {
    let that = this
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.withdraw({
      access_token: app.globalData.$TOKEN,
      amount: that.data.money
    }).then((res) => {
      wx.hideLoading()
      wx.showToast({
        title: "提现成功",
        icon: 'none',
        duration: 2000
      })
      wx.redirectTo({
        url: '/pages/closeAccount/closeAccount',
      })
    });


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})